package me.leiusual.sharing.ui.shop

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.Bank
import me.leiusual.sharing.pref.User
import me.leiusual.sharing.repository.BankRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-27
 * description:
 */
class ShopBankViewModel @Inject constructor(
    private val bankRepository: BankRepository,
    val user: User
) : ViewModel() {

  // 列表

  private val _bankList = MutableLiveData<Boolean>()
  val bankListFirst: LiveData<Boolean> = _bankList
  val bankList: LiveData<Data<List<Bank>>> = switchMap(_bankList) {
    bankRepository.bankList().toLiveData()
  }

  fun bankList(isFirst: Boolean) {
    _bankList.value = isFirst
  }

  // 删除

  private val _bankDelete = MutableLiveData<Long>()
  val bankDelete: LiveData<Data<Unit>> = switchMap(_bankDelete) {
    bankRepository.bankDelete(it).toLiveData()
  }

  fun bankDelete(baId: Long) {
    _bankDelete.value = baId
  }
}